package net.dotool.geo;

/**
 * 
 * @author 李岩飞
 * @email eliyanfei@126.com
 * @date 2020年5月8日上午11:09:39
 */
public class VectorArea {
	public double x;
	public double y;

	public VectorArea(double x, double y) {
		this.x = x;
		this.y = y;
	}

	VectorArea mutiply(double n) {
		return new VectorArea(x * n, y * n);
	}

	static double dot(VectorArea v, VectorArea w) {
		return v.x * w.x + v.y * w.y;
	}

	static double length(VectorArea v) {
		return Math.sqrt(dot(v, v));
	}

	static double angle(VectorArea v, VectorArea w) {
		return Math.acos(dot(v, w) / length(v) / length(w));
	}

	// 根据两角和公式推导
	static VectorArea rotate(VectorArea v, double rad) {
		return new VectorArea(v.x * Math.cos(rad) - v.y * Math.sin(rad), v.x * Math.sin(rad) + v.y * Math.cos(rad));
	}

	static double cross(VectorArea v, VectorArea w) {
		return v.x * w.y - v.y * w.x;
	}

	public static double area(PointArea a, PointArea b, PointArea c) {
		VectorArea v = new VectorArea(a.x - b.x, a.y - b.y);
		VectorArea w = new VectorArea(a.x - c.x, a.y - c.y);
		return cross(v, w);
	}

	static PointArea getLineIntersection(PointArea p, VectorArea v, PointArea q, VectorArea w) {
		VectorArea u = p.substract(q);
		double t = cross(w, u) / cross(v, w);
		v = v.mutiply(t);
		PointArea point = PointArea.create(v.x, v.y);
		return PointArea.create(p.x + point.x, p.y + point.y);
	}
}
